﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JBeamLibrary.Tools
{
    public class ErrorManager
    {
        #region Fields

        private readonly Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();

        #endregion

        #region Properties

        public Dictionary<string, List<string>> Errors
        {
            get { return _errors; }
        }

        public bool HasErrors
        {
            get { return _errors.Count > 0; }
        }

        #endregion

        #region CTORs

        public ErrorManager()
        {
        }

        #endregion

        #region Methods

        internal void AddError(string objectName, string message)
        {
            if (!_errors.ContainsKey(objectName))
                _errors.Add(objectName, new List<string>());
            _errors[objectName].Add(message);
        }

        internal void ClearErrors()
        {
            _errors.Clear();
        }

        public string GetErrorMessage(string title)
        {
            var builder = new StringBuilder();
            builder.AppendFormat("{0}:{1}{1}", title, Environment.NewLine, Environment.NewLine);
            foreach (var pair in _errors)
            {
                foreach (var msg in pair.Value)
                {
                    builder.AppendFormat("{0}: {1}{2}{2}", pair.Key, msg, Environment.NewLine);
                }
            }
            ClearErrors();
            return builder.ToString();
        }

        #endregion
    }
}
